// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import './assets/js/flexible';
import Vue from 'vue';
import Vant from 'vant';
// import '../../../static/mobile/css/vant/index.css'
import 'vant/lib/index.css';
import App from './App.vue';
import router from './router';
import store from './store';
import filters from './filters';
import  './types/constant';
import customRouter from './plugins/custom_router';
// import VueClipboard from 'vue-clipboard2';
// import uploader from 'vue-simple-uploader';
// import preview from 'vue-photo-preview';
// import 'vue-photo-preview/dist/skin.css';
import './assets/css/common.css';
// Vue.use(Lazyload,{listenEvents:['scroll']});
// Vue.use(preview);
// Vue.use(VueClipboard);
// Vue.use(uploader);
Vue.use(Vant);
Vue.use(customRouter);
Vue.config.productionTip = false;

// 注册全局自定义过滤器
Object.keys(filters).forEach(v => {
  Vue.filter(v, filters[v]);
});

/**
 * 处理iOS 微信客户端6.7.4 键盘收起页面未下移bug
 */
(/iphone|ipod|ipad/i.test(navigator.appVersion)) && document.addEventListener('blur', (e) => {
  document.body.scrollIntoView(false)
}, true);

function setBorwserEnv(){
  //判断是否是微信客户端环境
  let UA = navigator.userAgent.toLowerCase();
  if (UA.match(/MicroMessenger/i) == "micromessenger") {
    localStorage.setItem('borwserenv','wechat');
    return;
  }
//判断是否是支付宝客户端环境
  if (UA.match(/alipayclient/i) == "alipayclient") {
    localStorage.setItem('borwserenv','alipay');
    return;
  }
  localStorage.setItem('borwserenv','');
}

setBorwserEnv();


router.beforeEach((to, from, next) => {
  var htmls = document.getElementsByTagName('html');

  let cachPagesStr = store.state.cachPages;
  let cachPagesArry = cachPagesStr.split(',');
  let url = to.path;
  let name = url.substr(1);
  let findIndex = cachPagesArry.findIndex(elm=>elm == name);
  //通过浏览器返回进入
  if(findIndex != -1) {
    //需要返回刷新页面
    if(to.meta.backRefresh){
      cachPagesArry.splice(findIndex,1);
      store.state.cachPages = cachPagesArry.join(',');
    }
    //不可后退到下单页面
    if(to.path == '/order'){
      next({ replace: true,path: '/index' });
    }
  //通过$nocacheRouter进入
  }else {
    cachPagesArry.push(name);
    store.state.cachPages = cachPagesArry.join(',');
  }

  if (to.meta.bodybg) {
    htmls[0].style.backgroundColor = to.meta.bodybg;
    document.body.style.backgroundColor = to.meta.bodybg;
  }else{
    htmls[0].style.backgroundColor = '';
    document.body.style.backgroundColor = '';
  }
  // if (to.path == '/login') {
  //   sessionStorage.removeItem('user');
  // }
  let user = sessionStorage.getItem('user');
  // if (!user && to.path != '/login') {
  //   next({ path: '/login' })
  // } else {
  //   next();
  // }
  next();
});
router.afterEach((to, from, next) => {
  document.title = to.meta.title;
});
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
  template: '<App/>',
  components: { App }
});
